package com.dancun.controller;


import com.dancun.entity.Teacher;
import com.dancun.service.ITeacherService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController

public class TeacherController {
    @Autowired
    ITeacherService teacherService;

    @RequestMapping("/teacherlist")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("/Main/index.jsp");
        List<Teacher> tea = teacherService.list();
        mav.addObject("list", tea);
        return mav;
    }

    @RequestMapping("/tealogin")
    public ModelAndView login(Teacher tea) {
        ModelAndView mav = new ModelAndView("/Main/index.jsp");
        Map<String, Object> map = new HashMap<>();
        map.put("tno", tea.getTno());
        map.put("tpwd", tea.getTpwd());
        if (teacherService.listByMap(map).size() != 0) {
            Teacher teacher = teacherService.listByMap(map).get(0);
            mav.addObject("teacher", teacher);
            return mav;
        }
        mav.setViewName("/TeaLogin.html");
        return mav;
    }

    @RequestMapping("/teasigup")
    public ModelAndView sigup(Teacher tea) {
        ModelAndView mav = new ModelAndView("/Main/index.jsp");
        if (teacherService.save(tea)) {
            mav.addObject("teacher", tea);
            return mav;
        }

        mav.setViewName("/TeaSigup.html");
        return mav;
    }

    @RequestMapping("/teacherpersonal")
    public ModelAndView personal(Teacher tea) {//瀛︾敓涓汉淇℃伅
        ModelAndView mav = new ModelAndView("/Main/Personal.jsp");
        tea = (Teacher) teacherService.getById(tea.getTno());
        mav.addObject("tea", tea);
        return mav;
    }

    @RequestMapping("/teacherlistjson")
    public String listStu() throws IOException {
        List<Teacher> list = teacherService.list();
        JSONArray jsonarray = new JSONArray();
        String[][] str = new String[list.size()][2];
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();
        for (Teacher s : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("tno", s.getTno());
            jsonObject.put("tname", s.getTname());
            jsonarray.add(jsonObject);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("teacher", jsonarray);

        return jsonObject.toString();
    }

    @RequestMapping("/updateteacher")
    public ModelAndView update(Teacher tea) {
        ModelAndView mav = new ModelAndView("/listTeacher");
        if (teacherService.updateById(tea)) {
            return mav;
        }
        mav.setViewName("/listTeacher");
        return mav;
    }

    @RequestMapping("/delteacher")
    public String delete(@Param("id") String tno, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (teacherService.removeById(tno)) {
            out.print("<script>alert('删除成功');window.location.href='listTeacher'</script>");
        } else {
            out.print("<script>alert('删除失败');window.location.href='listTeacher'</script>");
        }
        return null;

    }
}
